<?php
// $Id: qcategory.class.php 10 2011-03-19 03:59:28Z tomqin@gmail.com $

/**
 * QCategory 无限父子类结构
 *
 * @author TomQin <tomqin@gmail.com>
 * @copyright Copyright (c) 2009-2012 SupeFun.Com Inc.
 * @package Core
 */

class QCategory{
	/* 全部節點 */
	public $nodesArray = array();

	/* 父/子數組 */
	public $childrenList = array();

	/**
	 * 初始化
	 */
	public function __construct() {}

	/**
	 * 清空節點
	 */
	public function clearNodes() {
		$this->nodesArray  = array();
		$this->childrenList = array();
	}

	/**
	 * 創建新節點
	 *
	 * @param $node  int/object  節點ID/對像
	 * @param $pid   int     父節點
	 * @param $name  string  節點標題
	 * @param $data  array   其他数据
	 * @return void
	 */
	public function addNode($node, $pid = 0, $name = '', $data = array()) {
		if($node instanceof QNode) {
			$this->nodesArray[$node->id] = $node;
			$id = $node->id;
		}else{
			$id = $node;
			$this->nodesArray[$id] = new QNode($id, $pid, $name, $data);
		}

		//绑定父ID
		$this->childrenList[$pid][$id] = $id;
	}

	/**
	 * 取得所有祖先
	 *
	 * @param $nodeId int 节点
	 * @return array 祖先的数组
	 */
	public function getAncestor($nodeId) {
		$ancestor = array();
		while(1){
			$node = $this->getNode($nodeId);
			$ancestor[] = $node;
			$nodeId = $node->pid;
			if($nodeId<=0){
				break;
			}
		}
		return array_reverse($ancestor);
	}

	/**
	 * 取得当前节点
	 *
	 * @param $nodeId int 节点
	 * @return QNode 节点
	 */
	public function getNode($nodeId) {
		return isset($this->nodesArray[$nodeId]) ? $this->nodesArray[$nodeId] : null;
	}

	/**
	 * 是否有子节点
	 *
	 * @param $nodeId int 节点ID
	 * @return bool
	 */
	public function hasChildren($nodeId) {
		return isset($this->childrenList[$nodeId]) && is_array($this->childrenList[$nodeId]);
	}
	/**
	 * 遞歸生成樹的 HTML
	 *
	 * @param $id  第一層分類ID
	 * @param $sid 選中層的類別ID

	public function getHtml($id){
		$html = '';
		$isNode = isset($this->nodeArray[$id]);
		if($this->hasChildren($id)) {
			$html .= $isNode ? '<li>'.$this->getNodeHtml($id) : '';
			$html .= '<ul>';
			foreach($this->plist[$id] as $k=>$cid){
				$html .= $this->getHtml($cid);
			}
			$html .= '</ul>';
			$html .= $isNode ? '</li>' : '';
 		} else {
 			if($isNode) {
				$html .= '<li>';
				$html .= $this->getNodeHtml($id);
				$html .= '</li>';
			}
		}
		return $html;
	}

	private function getNodeHtml($id) {
		$class = $this->hasChildren($id) ? 'folder' : 'file';
		$_node = $this->nodeArray[$id];
		$ref   = $id.','.$_node->pid;
		$url   = (isset($_node->url) && !empty($_node->url)) ? $_node->url : 'javascript:void(0)';
		$sel   = ($id == $this->selectedId) ? ' class="selected"': '';
		return "\r\n\t<span class=\"{$class}\" id=\"sort{$id}\"><a href=\"{$url}\" ref=\"{$ref}\"{$sel}> ".htmlspecialchars($_node->name)." </a></span>\r\n";
	}
*/

}

/**
 * Node 樹狀結構節點
 */
class QNode{
	public $id;				//ID
	public $pid;			//父ID
	public $name;			//名字
	public $data = array(); //其他数据

	function __construct($id, $pid = 0, $name = '', $data = array()) {
		$this->id = $id;
		$this->pid = $pid;
		$this->name = $name;
		$this->data = $data;
	}
}
?>
